package com.lans.linear;

/**
 * @author: lans
 * @date: 2025/2/23
 * @name: 刘宇
 */
public class ArrayStack {
    //栈
    private int[] stackArr;

    //大小
    private int maxStack;

    //指针
    private int top = -1;

    public ArrayStack(int maxStack) {
        this.maxStack = maxStack;
        this.stackArr = new int[maxStack];
    }
    //判断栈是否已满
    public boolean isFull(){
        return top == maxStack-1;
    }

    // 判断是不是空的
    public boolean isEmpty(){
        return top == -1;
    }

    //压栈
    public void push(int item){
        if (isFull()) {
            System.out.println("Stack is full");
        }else{
            stackArr[++top] = item;
        }
    }

    //弹栈
    public int pop(){
        if (isEmpty()) {
            throw new RuntimeException("Stack is empty,没哦有数据");
        }
        //记录
        int value = stackArr[top];
        //长度-短
        top--;
        return value;
    }

    //查询栈中的数据
    public void show(){
        if (isEmpty()) {
            throw new RuntimeException("Stack is empty");
        }
        for(int i = 0;i<top;i++){
            System.out.printf("stack[%d]=%d\n", i, stackArr[i]);
        }
    }

    public static void main(String[] args) {
        ArrayStack arrayStack = new ArrayStack(5);
        arrayStack.push(10);
        arrayStack.push(11);
        arrayStack.push(12);
        arrayStack.push(13);
        arrayStack.push(15);
        arrayStack.push(14);
        arrayStack.show();
    }
}